using System.Text;
using System;
using System.Data;
using System.Data.SqlClient;
using Csla;
using Csla.Data;
using Csla.Validation;

namespace BlazeApps.Library.Options
{ 
	[Serializable()] 
	public class OptionDetail : Csla.BusinessBase<OptionDetail>
	{
        private BlazeApps.Library.ConfigSettings Config = new BlazeApps.Library.ConfigSettings();
        private BA.DAL.DBUtility DAL = new BA.DAL.DBUtility();
        private Procedures Procs = new Procedures();

		#region Business Properties and Methods

		//declare members
		private Guid _Optionsid = Guid.NewGuid();
		private Guid _itemID = Guid.Empty;
		private bool _allowSummary = false;
		private bool _allowComments = false;
		private bool _allowTags = false;
		private bool _allowSubscriptions = false;
		private bool _allowRanking = false;
		private bool _allowInfoTools = false;
        private Boolean _hasdata = false;

		[System.ComponentModel.DataObjectField(true, false)]
		public Guid OptionsID
		{
			get
			{
				CanReadProperty("OptionsID", true);
				return _Optionsid;
			}
		}

		public Guid ItemID
		{
			get
			{
				CanReadProperty("ItemID", true);
				return _itemID;
			}
			set
			{
				CanWriteProperty("ItemID", true);
				if (!_itemID.Equals(value))
				{
					_itemID = value;
					PropertyHasChanged("ItemID");
				}
			}
		}

		public bool AllowSummary
		{
			get
			{
				CanReadProperty("AllowSummary", true);
				return _allowSummary;
			}
			set
			{
				CanWriteProperty("AllowSummary", true);
				if (!_allowSummary.Equals(value))
				{
					_allowSummary = value;
					PropertyHasChanged("AllowSummary");
				}
			}
		}

		public bool AllowComments
		{
			get
			{
				CanReadProperty("AllowComments", true);
				return _allowComments;
			}
			set
			{
				CanWriteProperty("AllowComments", true);
				if (!_allowComments.Equals(value))
				{
					_allowComments = value;
					PropertyHasChanged("AllowComments");
				}
			}
		}

		public bool AllowTags
		{
			get
			{
				CanReadProperty("AllowTags", true);
				return _allowTags;
			}
			set
			{
				CanWriteProperty("AllowTags", true);
				if (!_allowTags.Equals(value))
				{
					_allowTags = value;
					PropertyHasChanged("AllowTags");
				}
			}
		}

		public bool AllowSubscriptions
		{
			get
			{
				CanReadProperty("AllowSubscriptions", true);
				return _allowSubscriptions;
			}
			set
			{
				CanWriteProperty("AllowSubscriptions", true);
				if (!_allowSubscriptions.Equals(value))
				{
					_allowSubscriptions = value;
					PropertyHasChanged("AllowSubscriptions");
				}
			}
		}

		public bool AllowRanking
		{
			get
			{
				CanReadProperty("AllowRanking", true);
				return _allowRanking;
			}
			set
			{
				CanWriteProperty("AllowRanking", true);
				if (!_allowRanking.Equals(value))
				{
					_allowRanking = value;
					PropertyHasChanged("AllowRanking");
				}
			}
		}

		public bool AllowInfoTools
		{
			get
			{
				CanReadProperty("AllowInfoTools", true);
				return _allowInfoTools;
			}
			set
			{
				CanWriteProperty("AllowInfoTools", true);
				if (!_allowInfoTools.Equals(value))
				{
					_allowInfoTools = value;
					PropertyHasChanged("AllowInfoTools");
				}
			}
		}

        public Boolean hasdata
        {
            get
            {
                return _hasdata;
            }
        }
 
		protected override object GetIdValue()
		{
			return _Optionsid;
		}

		#endregion //Business Properties and Methods

		#region Validation Rules
		private void AddCustomRules()
		{
			//add custom/non-generated rules here...
		}

		private void AddCommonRules()
		{

		}

		protected override void AddBusinessRules()
		{
			AddCommonRules();
			AddCustomRules();
		}
		#endregion //Validation Rules

		#region Authorization Rules
		protected override void AddAuthorizationRules()
		{
			//TODO: Define authorization rules in Options
			//AuthorizationRules.AllowRead("ID", "OptionsReadGroup");
			//AuthorizationRules.AllowRead("ItemID", "OptionsReadGroup");
			//AuthorizationRules.AllowRead("AllowSummary", "OptionsReadGroup");
			//AuthorizationRules.AllowRead("AllowComments", "OptionsReadGroup");
			//AuthorizationRules.AllowRead("AllowTags", "OptionsReadGroup");
			//AuthorizationRules.AllowRead("AllowSubscriptions", "OptionsReadGroup");
			//AuthorizationRules.AllowRead("AllowRanking", "OptionsReadGroup");
			//AuthorizationRules.AllowRead("AllowInfoTools", "OptionsReadGroup");

			//AuthorizationRules.AllowWrite("ItemID", "OptionsWriteGroup");
			//AuthorizationRules.AllowWrite("AllowSummary", "OptionsWriteGroup");
			//AuthorizationRules.AllowWrite("AllowComments", "OptionsWriteGroup");
			//AuthorizationRules.AllowWrite("AllowTags", "OptionsWriteGroup");
			//AuthorizationRules.AllowWrite("AllowSubscriptions", "OptionsWriteGroup");
			//AuthorizationRules.AllowWrite("AllowRanking", "OptionsWriteGroup");
			//AuthorizationRules.AllowWrite("AllowInfoTools", "OptionsWriteGroup");
		}


		public static bool CanGetObject()
		{
			//TODO: Define CanGetObject permission in Options
			return true;
			//if (Csla.ApplicationContext.User.IsInRole("OptionsViewGroup"))
			//	return true;
			//return false;
		}

		public static bool CanAddObject()
		{
			//TODO: Define CanAddObject permission in Options
			return true;
			//if (Csla.ApplicationContext.User.IsInRole("OptionsAddGroup"))
			//	return true;
			//return false;
		}

		public static bool CanEditObject()
		{
			//TODO: Define CanEditObject permission in Options
			return true;
			//if (Csla.ApplicationContext.User.IsInRole("OptionsEditGroup"))
			//	return true;
			//return false;
		}

		public static bool CanDeleteObject()
		{
			//TODO: Define CanDeleteObject permission in Options
			return true;
			//if (Csla.ApplicationContext.User.IsInRole("OptionsDeleteGroup"))
			//	return true;
			//return false;
		}
		#endregion //Authorization Rules

		#region Factory Methods
		private OptionDetail()
		{ /* require use of factory method */ }

		public static OptionDetail NewOptions()
		{
			if (!CanAddObject())
				throw new System.Security.SecurityException("User not authorized to add a Options");
			return DataPortal.Create<OptionDetail>();
		}

		public static OptionDetail GetOptions(Guid id)
		{
			if (!CanGetObject())
				throw new System.Security.SecurityException("User not authorized to view a Options");
			return DataPortal.Fetch<OptionDetail>(new Criteria(id));
		}

		public static void DeleteOptions(Guid id)
		{
			if (!CanDeleteObject())
				throw new System.Security.SecurityException("User not authorized to remove a Options");
			DataPortal.Delete(new Criteria(id));
		}

		public override OptionDetail Save()
		{
			if (IsDeleted && !CanDeleteObject())
				throw new System.Security.SecurityException("User not authorized to remove a Options");
			else if (IsNew && !CanAddObject())
				throw new System.Security.SecurityException("User not authorized to add a Options");
			else if (!CanEditObject())
				throw new System.Security.SecurityException("User not authorized to update a Options");

			return base.Save();
		}

		#endregion //Factory Methods

		#region Data Access

		#region Criteria

		[Serializable()]
		private class Criteria
		{
			public Guid ItemID;

			public Criteria(Guid Itemid)
			{
				this.ItemID = Itemid;
			}
		}

		#endregion //Criteria

		#region Data Access - Create
		[RunLocal]
		protected override void DataPortal_Create()
		{
			ValidationRules.CheckRules();
		}

		#endregion //Data Access - Create

		#region Data Access - Fetch
        private void DataPortal_Fetch(Criteria criteria)
        {
            ExecuteFetch(criteria);
        }

        private void ExecuteFetch(Criteria criteria)
        {
            DAL.CreateDBObjects(Config.ConnectionString, Config.Provider);

            DAL.AddParameter("@Itemid", criteria.ItemID);

            using(SafeDataReader dr = new SafeDataReader(DAL.ExecuteReader(Procs.GetOptionByItemID.ToString().Trim(), CommandType.Text, ConnectionState.Open)))
            {
                FetchObject(dr);
                ValidationRules.CheckRules();

                //load child object(s)
                FetchChildren(dr);
            }
        }

        private void FetchObject(SafeDataReader dr)
        {
            if (dr.Read())
            {
                _Optionsid = dr.GetGuid("OptionsID");
                _itemID = dr.GetGuid("ItemID");
                _allowSummary = dr.GetBoolean("AllowSummary");
                _allowComments = dr.GetBoolean("AllowComments");
                _allowTags = dr.GetBoolean("AllowTags");
                _allowSubscriptions = dr.GetBoolean("AllowSubscriptions");
                _allowRanking = dr.GetBoolean("AllowRanking");
                _allowInfoTools = dr.GetBoolean("AllowInfoTools");
                _hasdata = true;
            }
            else
            {
                _hasdata = false;
            }
		}

		private void FetchChildren(SafeDataReader dr)
		{
		}
		#endregion //Data Access - Fetch

		#region Data Access - Insert
        protected override void DataPortal_Insert()
        {
            ExecuteInsert();
        }

        private void ExecuteInsert()
        {
            DAL.CreateDBObjects(Config.ConnectionString, Config.Provider);

            AddInsertParameters(ref DAL);

            DAL.ExecuteNonQuery(Procs.InsertOption.ToString().Trim(), CommandType.Text, ConnectionState.Open);
        }

        private void AddInsertParameters(ref BA.DAL.DBUtility DAL)
        {
			DAL.AddParameter("@OptionsID", _Optionsid);
			DAL.AddParameter("@ItemID", _itemID);
			DAL.AddParameter("@AllowSummary", _allowSummary);
			DAL.AddParameter("@AllowComments", _allowComments);
			DAL.AddParameter("@AllowTags", _allowTags);
			DAL.AddParameter("@AllowSubscriptions", _allowSubscriptions);
			DAL.AddParameter("@AllowRanking", _allowRanking);
			DAL.AddParameter("@AllowInfoTools", _allowInfoTools);
		}
		#endregion //Data Access - Insert

		#region Data Access - Update
        protected override void DataPortal_Update()
        {
            if (base.IsDirty)
            {
                ExecuteUpdate();
            }
        }

        private void ExecuteUpdate()
        {
            DAL.CreateDBObjects(Config.ConnectionString, Config.Provider);

            AddUpdateParameters(ref DAL);

            DAL.ExecuteNonQuery(Procs.UpdateOption.ToString().Trim(), CommandType.Text, ConnectionState.Open);
        }

        private void AddUpdateParameters(ref BA.DAL.DBUtility DAL)
        {
			DAL.AddParameter("@OptionsID", _Optionsid);
			DAL.AddParameter("@ItemID", _itemID);
			DAL.AddParameter("@AllowSummary", _allowSummary);
			DAL.AddParameter("@AllowComments", _allowComments);
			DAL.AddParameter("@AllowTags", _allowTags);
			DAL.AddParameter("@AllowSubscriptions", _allowSubscriptions);
			DAL.AddParameter("@AllowRanking", _allowRanking);
			DAL.AddParameter("@AllowInfoTools", _allowInfoTools);
		}

		private void UpdateChildren(SqlConnection cn)
		{
		}
		#endregion //Data Access - Update

		#region Data Access - Delete
        protected override void DataPortal_DeleteSelf()
        {
            DataPortal_Delete(new Criteria(_Optionsid));
        }

        private void DataPortal_Delete(Criteria criteria)
        {
            ExecuteDelete(criteria);
        }

        private void ExecuteDelete(Criteria criteria)
        {
            DAL.CreateDBObjects(Config.ConnectionString, Config.Provider);

            AddDeleteParameters(ref DAL, criteria);

            DAL.ExecuteNonQuery(Procs.DeleteOption.ToString().Trim(), CommandType.Text, ConnectionState.Open);
        }

        private void AddDeleteParameters(ref BA.DAL.DBUtility DAL, Criteria criteria)
        {
            DAL.AddParameter("@OptionsID", criteria.ItemID);
        }
		#endregion //Data Access - Delete
		#endregion //Data Access
	}
}
